package com.naver.jts.test;

import javax.swing.JFrame;

import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.geotools.referencing.ReferencingFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import com.naver.jts.JTSDrawingPanel;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.MultiPoint;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.WKTReader;

public class SimpleWktTest2 {
	public static void main(String[] args) throws Exception {
		JTSDrawingPanel panel = new JTSDrawingPanel();
		JFrame frame = new JFrame("Draw geometries");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(panel);
		frame.setSize(500, 500);

		WKTReader reader = new WKTReader();

		MultiPoint points = (MultiPoint)reader.read("MULTIPOINT (38850.2156 57924.5932, 38745.6287 56729.1595, 38805.4004 56723.9302, 38631.0889 54731.5408, 38571.3172 54736.7701, 38466.7303 53541.3365, 38526.502 53536.1072, 38352.1417 51543.7218, 38292.4188 51548.9471, 38187.8319 50353.5135, 38885.1682 50292.5045, 38872.0949 50143.0753, 39669.0506 50073.3506, 39682.124 50222.7799, 40379.4603 50161.7708, 40460.0829 51083.2914, 40400.3112 51088.5207, 40792.5021 55571.3162, 40852.2738 55566.0869, 40932.8964 56487.6074, 41829.4716 56409.1673, 41839.9303 56528.7106, 46322.8065 56136.5098, 46312.3478 56016.9664, 47208.923 55938.5262, 47219.3817 56058.0696, 51702.2578 55665.8688, 51691.7992 55546.3254, 53385.3301 55398.1606, 53494.2748 56643.404, 38850.2156 57924.5932)");
		
		final String UTMK = "PROJCS[\"PCS_ITRF2000_TM\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"Semi_Major\",6378137.0],PARAMETER[\"Semi_Minor\",6356752.314140356],PARAMETER[\"Central_Meridian\",127.5],PARAMETER[\"Latitude_Of_Origin\",38.0],PARAMETER[\"Scale_Factor\",0.9996],PARAMETER[\"False_Easting\",1000000.0],PARAMETER[\"False_Northing\",2000000.0],UNIT[\"metre\",1.0]]";
		final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
		CoordinateReferenceSystem sourceCRS = DefaultGeographicCRS.WGS84;
		CoordinateReferenceSystem targetCRS = ReferencingFactoryFinder.getCRSFactory(null).createFromWKT(UTMK); 
		MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS);
		Point point = (Point)JTS.transform(geometryFactory.createPoint(new Coordinate(126.98046555,37.52360718)), transform);

		StringBuffer output = new StringBuffer("MULTIPOINT(");
		double x = point.getX() * 100d;
		double y = point.getY() * 100d;
		
		String sourcePoints = "38850.2156 57924.5932,38745.6287 56729.1595,38805.4004 56723.9302,38631.0889 54731.5408,38571.3172 54736.7701,38466.7303 53541.3365,38526.502 53536.1072,38352.1417 51543.7218,38292.4188 51548.9471,38187.8319 50353.5135,38885.1682 50292.5045,38872.0949 50143.0753,39669.0506 50073.3506,39682.124 50222.7799,40379.4603 50161.7708,40460.0829 51083.2914,40400.3112 51088.5207,40792.5021 55571.3162,40852.2738 55566.0869,40932.8964 56487.6074,41829.4716 56409.1673,41839.9303 56528.7106,46322.8065 56136.5098,46312.3478 56016.9664,47208.923 55938.5262,47219.3817 56058.0696,51702.2578 55665.8688,51691.7992 55546.3254,53385.3301 55398.1606,53494.2748 56643.404,38850.2156 57924.5932";
		String[] split = sourcePoints.split(",");
		
		for (String s : split) {
			String[] p = s.split(" ");
			
			double x1 = (x + Double.parseDouble(p[0]))/100;
			double y1 = (y + Double.parseDouble(p[1]))/100;
			
			
			sourceCRS = ReferencingFactoryFinder.getCRSFactory(null).createFromWKT(UTMK); 
			targetCRS = DefaultGeographicCRS.WGS84;
			transform = CRS.findMathTransform(sourceCRS, targetCRS);
			point = (Point)JTS.transform(geometryFactory.createPoint(new Coordinate(x1, y1)), transform);
			
			output.append(point.getX()).append(" ").append(point.getY()).append(",");
		}
		output = output.delete(output.length()-1, output.length()).append(")");
		
		
//		panel.addGeometry(points);
//		frame.setVisible(true);
	}
}
